<template>
<div>
  <el-button>
    添加
  </el-button>
  <el-table
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children'}">
    <el-table-column
        prop="label"
        label="菜单名称"
        width="180">
    </el-table-column>
    <el-table-column
        prop="id"
        label="菜单编号"
        width="180">
    </el-table-column>
    <el-table-column
        prop="path"
        label="菜单路径">
    </el-table-column>
    <el-table-column
        width="100"
        label="菜单图标">
      <template #default="{ row }">
        <i :class="row.icon"></i>
      </template>
    </el-table-column>
    <el-table-column
        width="300"
        label="操作">
      <template #default="{ row }">
          <el-button @click="del(row.id)" size="mini" type="danger">
            删除
          </el-button>
          <el-button size="mini" type="success">
            编辑
          </el-button>
          <el-button @click="addC(row.id,row.path)" size="mini" v-if="row.children.length">
            添加子菜单
          </el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog width="60%" title="添加菜单"
             :visible.sync="dialogFormVisible">
    <el-form :model="form">
      <el-form-item label="菜单名称" :label-width="formLabelWidth">
        <el-input v-model="form.title" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单路径" :label-width="formLabelWidth">
        <el-input placeholder="请输入菜单路径" v-model="form.path">
          <template slot="prepend"> {{ parentPath }}</template>
        </el-input>
      </el-form-item>
      <el-form-item label="菜单图标" :label-width="formLabelWidth">
        <div style="height: 50px;width:50px;font-size: 30px">
          <i :class="form.icon"></i>
        </div>
        <el-button @click="dialogIconVisible = true">选择图标</el-button>
      </el-form-item>
      <el-form-item label="菜单类型" :label-width="formLabelWidth">
        <el-select v-model="form.menuType" placeholder="请选择菜单类型">
          <el-option
              v-for="i of menuType"
              :key="i.value"
              :label="i.title"
              :value="i.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
  <el-dialog  title="选择图标" width="80%" :visible.sync="dialogIconVisible">
    <el-scrollbar style="height:60vh">
      <ul class="iconbox">
        <li class="iconitem" v-for="i of iconList"
            @click="selectIcon(i)"
            :key="i">
          <i :class="i"></i>
        </li>
      </ul>
    </el-scrollbar>
  </el-dialog>
</div>
</template>

<script>
import { menuInfo,delMenu ,addMenu} from "../../api";
import { iconList } from "../../ulits/icon";

export default {
  data(){
    return{
      iconList,
      list:[],
      dialogFormVisible:false,
      dialogIconVisible:false,
      parentPath:"",
      form:{
        title:'',
        path:"",
        menuType:"",
        icon:""
      },
      menuType:[{
        title:"菜单",
        value:'M'
      },{
        title:"按钮",
        value:'B'
      }],
      formLabelWidth:'100px'
    }
  },
  created() {
    this.getList()
  },
  methods:{
    async getList(){
      const res = await menuInfo(this.$store.getters.roleId)
      console.log(res)
      this.list = res.data.data
    },
    selectIcon(iconName){
      this.form.icon = iconName
      this.dialogIconVisible = false
    },
    addC(id,path){
      this.dialogFormVisible = true
      this.form.parentId = id
      this.parentPath = path
    },
    async submit(){
      console.log(this.form)
      this.form.path = this.parentPath+this.form.path
      const res = await addMenu(this.form)
      console.log(res)
    },
    del(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await delMenu(id)
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        await this.getList()
        console.log(res)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>
.iconbox{
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}
.iconitem{
  list-style: none;
  width: 100px;
  height: 100px;
  margin: 4px;
  font-size: 40px;
  text-align: center;
  line-height: 100px;
  box-sizing: border-box;
  border: 1px solid #E9EEF3;
}
</style>
